home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / xview / genial / func / integrate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-14  |  11.3 KB  |  411 lines

  1.  
  2. /*
  3.  * integrate.c -- routines for trace integration
  4.  */
  5.  
  6. #include "display.h"
  7. #include "ui.h"
  8. #include "log.h"
  9. #include "reg.h"
  10. #include "scale.h"
  11. #include "trace_ui.h"
  12.  
  13. GC        igc;
  14. extern GC trgc;
  15.  
  16. static trace_intgwin_objects *trace_iwin = NULL;
  17.  
  18. static struct trcontext *curtrace;
  19. struct trcontext *trace_by_panelwin();
  20.  
  21. /*********************************************************/
  22.  
  23. integ_init(ctx)
  24.     struct integ_context *ctx;
  25. {
  26.     int       i;
  27.  
  28.     for (i = 0; i < NINT; i++) {
  29.     ctx->ireg[i].min = ctx->ireg[i].max = ctx->ireg[i].isize = 0;
  30.     ctx->ireg[i].ival = NULL;
  31.     }
  32.     ctx->ipntw = ctx->iwin = ctx->icanv = NULL;
  33.     ctx->cnum = 0;
  34.     ctx->yglab = NULL;
  35.     ctx->cbl = (struct cbstore *) malloc(sizeof(struct cbstore));
  36.     ctx->cbr = (struct cbstore *) malloc(sizeof(struct cbstore));
  37.     ctx->cbl->cht = ctx->cbl->cwt = 0;
  38.     ctx->cbr->cht = ctx->cbr->cwt = 0;
  39.  
  40.  
  41. }
  42.  
  43. /***********************************************************/
  44. int
  45. integrate_eval_proc(item, event)
  46.     Panel_item item;
  47.     Event    *event;
  48. {
  49.     struct integ_context *ctx;
  50.     int id;
  51.  
  52. #ifdef DEBUG
  53.     fputs("trace: integrate_eval_proc: \n", stderr);
  54. #endif
  55.  
  56.     curtrace = trace_by_panelwin(event_window(event));
  57.     if (curtrace == NULL) {
  58.     printf("(integrate_eval_proc)no such trace!\n");
  59.     return -1;
  60.     }
  61.     ctx = &curtrace->integ;
  62.     if (ctx->ireg[ctx->cnum].max <= 0 ||
  63.     ctx->ireg[ctx->cnum].min <= 0)
  64.     return -1;
  65.  
  66.     comp_integ(curtrace);
  67.  
  68.     disp_integ(curtrace);
  69.  
  70. #ifdef MAKE_WORK_LATER        /* use this to do multiple integration
  71.                  * windows */
  72.     if (ctx->cnum < NINT - 2)
  73.     ctx->cnum++;
  74. #endif
  75.     return 0;
  76. }
  77.  
  78. /***********************************************************/
  79. comp_integ(trace)
  80.     struct trcontext *trace;
  81. {
  82.     int       i, j, sum;
  83.     struct integ_context *ctx;
  84.  
  85.     ctx = &trace->integ;
  86.     ctx->ireg[ctx->cnum].isize =
  87.     ctx->ireg[ctx->cnum].max - ctx->ireg[ctx->cnum].min;
  88.  
  89.     ctx->ireg[ctx->cnum].ival =
  90.     (int *) calloc(ctx->ireg[ctx->cnum].isize, sizeof(int));
  91.  
  92.     j = 0;
  93.     sum = 0;
  94.     for (i = ctx->ireg[ctx->cnum].min; i < ctx->ireg[ctx->cnum].max; i++) {
  95.     sum += trace->pbuf[i].oval;
  96.     ctx->ireg[ctx->cnum].ival[j] = sum;
  97.     j++;
  98.     }
  99. }
  100.  
  101. /***********************************************************/
  102. /* build a display for the integral stuff */
  103. disp_integ(trace)
  104.     struct trcontext *trace;
  105. {
  106.     char      title[80], message[80];
  107.     struct integ_context *ctx;
  108.     XGCValues gcval;
  109.     void      icanv_repaint_proc();
  110.  
  111.     ctx = &trace->integ;
  112.  
  113.     if (ctx->iwin == NULL) {
  114.     trace_iwin = trace_intgwin_objects_initialize(NULL, base_win->ctrlwin);
  115.  
  116.     ctx->iwin = trace_iwin->intgwin;
  117.     ctx->icanv = trace_iwin->intgcanv;
  118.     ctx->ipntw = canvas_paint_window(ctx->icanv);
  119.     ctx->ixid = (XID) xv_get(ctx->ipntw, XV_XID, NULL);
  120.  
  121.     /* set up a GC for the window */
  122.     gcval.foreground = BlackPixel(display, DefaultScreen(display));
  123.     gcval.background = WhitePixel(display, DefaultScreen(display));
  124.     gcval.clip_mask = None;
  125.     igc = XCreateGC(display, ctx->ixid,
  126.             GCForeground | GCBackground | GCClipMask, &gcval);
  127.     }
  128.     sprintf(title, "Integrate Trace: %d.%d", curfunc->id-1, ctx->cnum+1);
  129.     xv_set(ctx->iwin,
  130.        XV_WIDTH, MAX(ctx->ireg[ctx->cnum].isize + 85, 210),
  131.        XV_LABEL, title,
  132.        XV_SHOW, TRUE,
  133.        FRAME_CLOSED, FALSE,
  134.        NULL);
  135.     xv_set(ctx->icanv,
  136.        XV_WIDTH, ctx->ireg[ctx->cnum].isize + 85,
  137.        NULL);
  138.  
  139.     sprintf(message, "Integral Total: %d",
  140.         ctx->ireg[ctx->cnum].ival[ctx->ireg[ctx->cnum].isize - 1]);
  141.     xv_set(trace_iwin->message1,
  142.        PANEL_LABEL_STRING, message, NULL);
  143.     panel_paint(trace_iwin->message1, PANEL_CLEAR);
  144.  
  145.     icanv_repaint_proc(ctx->icanv, ctx->ipntw, display, ctx->ixid, NULL);
  146. }
  147.  
  148. /***********************************************************/
  149. /* * Repaint callback function for `icanv'.  */
  150. void
  151. icanv_repaint_proc(canvas, paint_window, display, xid, rects)
  152.     Canvas    canvas;
  153.     Xv_window paint_window;
  154.     Window    xid;
  155.     Xv_xrectlist *rects;
  156. {
  157.     XGCValues gcval;
  158.     struct integ_context *ctx, *find_integ();
  159.     float    range;
  160.     int      i, y, ylen, height;
  161.  
  162. #ifdef DEBUG
  163.     printf("icanv_repaint_proc \n");
  164. #endif
  165.  
  166.     if (curtrace == NULL)
  167.     return;
  168.  
  169.     ctx = &curtrace->integ;
  170.     if (ctx->ireg[ctx->cnum].ival == NULL)
  171.     return;
  172.  
  173.     XClearWindow(display, ctx->ixid);
  174.     if ((ylen = integ_axes(ctx)) <= 0)
  175.     return;
  176.  
  177.     height = (int)xv_get(ctx->icanv, XV_HEIGHT, NULL) - STARTY;
  178.     range = (float) (ctx->yglab[ylen - 1].val - ctx->yglab[0].val);
  179.  
  180.     gcval.foreground = BlackPixel((Display *) display,
  181.                   DefaultScreen((Display *) display));
  182.     XChangeGC(display, igc, GCForeground, &gcval);
  183.  
  184.     for (i = 0; i < ctx->ireg[ctx->cnum].isize; i++) {
  185.     y = height - (int)((height * ctx->ireg[ctx->cnum].ival[i]) / range);
  186.     XDrawLine(display, ctx->ixid, igc, i, y, i, height);
  187.     }
  188. }
  189.  
  190. /***********************************************************/
  191. integrate_event_proc(event, curtrace)
  192.     Event    *event;
  193.     struct trcontext *curtrace;
  194. {
  195.     struct integ_context *integ;
  196.     int       height, slen, dlen, x, y;
  197.     static int prev_left = -1, prev_right = -1;
  198.  
  199.     height = (int)xv_get(curtrace->win_info->trcanv, XV_HEIGHT, NULL);
  200.     slen = irint((double) height * SMPHT);
  201.     dlen = height - slen;
  202.  
  203.     x = event_x(event);
  204.     y = event_y(event);
  205.  
  206.     /* if they pressed the first button, draw a line in red */
  207.     integ = &curtrace->integ;
  208.     if (event_id(event) == BUT(1) && event_is_down(event)) {
  209.     if (integ->ireg[integ->cnum].max > 0 &&
  210.         x >= integ->ireg[integ->cnum].max)
  211.         return;
  212.     if (prev_left != -1)    /* undraw previous line */
  213.         undraw_intg_line(prev_left, curtrace, dlen);
  214.  
  215.     prev_left = x;
  216.     XSetForeground(display, trgc, pallet[RED].pixel);
  217.     XSetLineAttributes(display, trgc, 2, LineSolid, CapButt, JoinBevel);
  218.     XDrawLine(display, curtrace->trxid, trgc, x, 0, x, dlen);
  219.     integ->ireg[integ->cnum].min = x;
  220.  
  221.     move_cross_bar_left(x, integ, curtrace);
  222.     }
  223.     /* if they pressed the third button, draw the end line in red */
  224.     if (event_id(event) == BUT(3) && event_is_down(event)) {
  225.     if (integ->ireg[integ->cnum].min > 0 &&
  226.         x <= integ->ireg[integ->cnum].min)
  227.         return;
  228.     if (prev_right != -1)
  229.         undraw_intg_line(prev_right, curtrace, dlen);
  230.  
  231.     prev_right = x;
  232.     XSetForeground(display, trgc, pallet[RED].pixel);
  233.     XSetLineAttributes(display, trgc, 2, LineSolid, CapButt, JoinBevel);
  234.     XDrawLine(display, curtrace->trxid, trgc, x, 0, x, dlen);
  235.     integ->ireg[integ->cnum].max = x;
  236.  
  237.     move_cross_bar_right(x, integ, curtrace);
  238.     }
  239.     XSetLineAttributes(display, trgc, 1, LineSolid, CapButt, JoinBevel);
  240. }
  241.  
  242. /***************************************************************/
  243. move_cross_bar_left(x, integ, curtrace)
  244.     int       x;
  245.     struct integ_context *integ;
  246.     struct trcontext *curtrace;
  247. {
  248.     XPoint    pt;
  249.     XGCValues gcval;
  250.  
  251.     /* undraw previous cross bar */
  252.     if (integ->cbl->cht != 0 || integ->cbl->cwt != 0)
  253.     /* refresh image under cross bar */
  254.     ref_cb(img_win->d_xid, integ->cbl);
  255.     draw_cpl();            /* redraw crosses in point list */
  256.     draw_pvec(img_win->d_xid, curtrace->pbuf, curtrace->npts);
  257.  
  258.     /* draw marker on image */
  259.     pt.x = (short) curtrace->pbuf[x].pt.x;
  260.     pt.y = (short) curtrace->pbuf[x].pt.y;
  261.  
  262.     cbget(orig_ximg, integ->cbl, pt);
  263.     gcval.foreground = pallet[RED].pixel;
  264.     XChangeGC(display, gc, GCForeground, &gcval);
  265.     draw_cb(img_win->d_xid, integ->cbl);
  266. }
  267.  
  268. /***************************************************************/
  269. move_cross_bar_right(x, integ, curtrace)
  270.     int       x;
  271.     struct integ_context *integ;
  272.     struct trcontext *curtrace;
  273. {
  274.     XPoint    pt;
  275.     XGCValues gcval;
  276.  
  277.     /* undraw previous cross bar */
  278.     if (integ->cbr->cht != 0 || integ->cbr->cwt != 0)
  279.     /* refresh image under cross bar */
  280.     ref_cb(img_win->d_xid, integ->cbr);
  281.     draw_cpl();            /* redraw crosses in point list */
  282.     draw_pvec(img_win->d_xid, curtrace->pbuf, curtrace->npts);
  283.  
  284.     /* draw marker on image */
  285.     pt.x = (short) curtrace->pbuf[x].pt.x;
  286.     pt.y = (short) curtrace->pbuf[x].pt.y;
  287.  
  288.     cbget(orig_ximg, integ->cbr, pt);
  289.     gcval.foreground = pallet[RED].pixel;
  290.     XChangeGC(display, gc, GCForeground, &gcval);
  291.     draw_cb(img_win->d_xid, integ->cbr);
  292. }
  293.  
  294. /***********************************************************/
  295. undraw_intg_line(x, curtrace, dlen)
  296.     int       x;
  297.     struct trcontext *curtrace;
  298.     int       dlen;
  299. {
  300.     XGCValues gcval;
  301.     int       grheight, ylen;
  302.     double    scfac;
  303.  
  304. #ifdef DEBUG
  305.     printf("undrawing line at: %d \n", x);
  306. #endif
  307.  
  308.     /* shouldnt need 'drawaxes' every time..... -blt */
  309.     ylen = drawaxes(curtrace);
  310.     scfac = (double) (dlen - STARTY) /
  311.     (curtrace->yglab[ylen - 1].val - curtrace->yglab[0].val);
  312.  
  313.     gcval.foreground = WhitePixel((Display *) display,
  314.                   DefaultScreen((Display *) display));
  315.     XChangeGC(display, trgc, GCForeground, &gcval);
  316.     XSetLineAttributes(display, trgc, 2, LineSolid, CapButt, JoinBevel);
  317.  
  318.     XDrawLine(display, curtrace->trxid, trgc, x, 0, x, dlen);
  319.  
  320.     gcval.foreground = BlackPixel((Display *) display,
  321.                   DefaultScreen((Display *) display));
  322.     XChangeGC(display, trgc, GCForeground, &gcval);
  323.     XSetLineAttributes(display, trgc, 1, LineSolid, CapButt, JoinBevel);
  324.  
  325.     grheight = dlen - irint((double) (trace_value(x, curtrace)
  326.                       - curtrace->yglab[0].val) * scfac);
  327.     XDrawLine(display, curtrace->trxid, trgc, x, dlen + 1, x,
  328.           (int) grheight);
  329.  
  330.     /* one position below */
  331.     grheight = dlen - irint((double) (trace_value(x - 1, curtrace)
  332.                       - curtrace->yglab[0].val) * scfac);
  333.     XDrawLine(display, curtrace->trxid, trgc, x - 1, dlen + 1, x - 1,
  334.           (int) grheight);
  335.  
  336.     /* one position below */
  337.     grheight = dlen - irint((double) (trace_value(x + 1, curtrace)
  338.                       - curtrace->yglab[0].val) * scfac);
  339.     XDrawLine(display, curtrace->trxid, trgc, x + 1, dlen + 1, x + 1,
  340.           (int) grheight);
  341. }
  342.  
  343. /***********************************************************/
  344. struct integ_context
  345.          *
  346. find_integ(pntw)
  347.     Xv_window pntw;
  348. {
  349.     struct trcontext *trace;
  350.     struct integ_context *intg;
  351.     struct logent *log;
  352.  
  353.     log = loghead;
  354.     while (log != NULL) {
  355.     trace = log->trace;
  356.     if (trace != NULL) {
  357.         intg = &trace->integ;
  358.         if (intg->iwin == pntw) 
  359.         return intg;
  360.     }
  361.     log = log->next;
  362.     }
  363.     return NULL;
  364. }
  365.  
  366. /***********************************************************/
  367. int
  368. integ_axes(ctx)
  369.     struct integ_context *ctx;
  370. {
  371.     int       maxv;
  372.     int       ylen, width, height;
  373.  
  374.     if (ctx->ireg[ctx->cnum].ival == NULL)
  375.     return -1;
  376.     maxv = ctx->ireg[ctx->cnum].ival[ctx->ireg[ctx->cnum].isize - 1];
  377.  
  378.     width = (int)xv_get(ctx->icanv, XV_WIDTH, NULL);
  379.     height = (int)xv_get(ctx->icanv, XV_HEIGHT, NULL);
  380.     XSetForeground(display, igc, BlackPixel(display, DefaultScreen(display)));
  381.  
  382.     /* build_glab(min, max, max # labels, pixel length, return values) */
  383.     ylen = build_glab(0, maxv, 5, height - (STARTY * 2), &ctx->yglab);
  384.  
  385.     /*
  386.      * xlabelgraph(window stuff, # of labels, starting loc, width,height of
  387.      * window, orientation, direction
  388.      */
  389.     xlabelgraph(display, ctx->ixid, igc, ctx->yglab, ylen,
  390.      width - 80, height - STARTY, width, height - STARTY, VERTICAL, -1);
  391.     return ylen;
  392. }
  393.  
  394. /*********************************/
  395. integ_clear(trace)
  396.     struct trcontext *trace;
  397. {
  398.     struct integ_context *ctx;
  399.  
  400.     ctx = &trace->integ;
  401.     if (ctx->iwin != NULL)
  402.     xv_set(ctx->iwin, XV_SHOW, FALSE, NULL);
  403.  
  404.     if (ctx->cbl->cwt != 0 && ctx->cbl->cht != 0)
  405.             ref_cb(img_win->d_xid, ctx->cbl);
  406.  
  407.     if (ctx->cbr->cwt != 0 && ctx->cbr->cht != 0)
  408.             ref_cb(img_win->d_xid, ctx->cbr);
  409.  
  410. }
  411.